/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.manager;

import br.com.foxline.manager.search.ManagerViewCustomer;
import br.com.foxline.model.Message;
import br.com.foxline.model.Sale;
import br.com.foxline.service.MessageService;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author luciano
 */
@ManagedBean
@ViewScoped
public class ManagerInbox implements Serializable{
    
    @EJB
    private MessageService messageService;
    
    public List<Message> messages(){
        return messageService.messages();
    }
    
    public int count(){
        return messageService.countMessagesUnread();
    }
    
    public void readMessage(Message m){
        m.setWasRead(true);
        messageService.update(m);
        redirect("viewMessage.xhtml?messageId=" + m.getId());        
    }

     public void openSale(Sale s){
        
        redirect("approvesale.xhtml?saleId=" + s.getId());        
    }
    
    
    public void redirect(String string) {
        try {
            FacesContext context = FacesContext.getCurrentInstance();
            context.getExternalContext().redirect(string);
        } catch (IOException ex) {
            Logger.getLogger(ManagerViewCustomer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
}
